<!--  -->
<template>
  <div>
    <el-row>
      <el-col :span="24" class="page_box_top">
        <i class="el-icon-s-fold"></i>
        <el-breadcrumb separator="/" class="page_box_top_nav">
          <el-breadcrumb-item class="breadcrumb_active_item"
            >签到管理</el-breadcrumb-item
          >
          <el-breadcrumb-item
            :to="{ name: 'signlist' }"
            >新增签到活动列表</el-breadcrumb-item
          >
        </el-breadcrumb>
      </el-col>
    </el-row>
    <el-form :model="dialogForm" ref="ruleFormRef">
      <el-form-item label="标题:" prop="title" :label-width="formLabelWidth">
        <el-input v-model="dialogForm.title" autocomplete="off" />
      </el-form-item>
      <el-form-item
        label="图片:"
        prop="image_path"
        :label-width="formLabelWidth"
      >
        <el-upload
          v-model="dialogForm.image_path"
          class="dl-avatar-uploader-min square"
          action="#"
          :http-request="uploadFile"
          :show-file-list="false"
          :before-upload="beforeImageUpload"
          :on-success="handleUpImage"
          list-type="picture-card"
          accept="image/*"
        >
          <el-image :src="dialogForm.image_path" class="avatar" />
        </el-upload>
      </el-form-item>
      <el-form-item
        label="比赛开始日期:"
        prop="title"
        :label-width="formLabelWidth"
      >
        <el-input v-model="dialogForm.start_date" autocomplete="off" />
      </el-form-item>
      <el-form-item
        label="比赛地址:"
        prop="title"
        :label-width="formLabelWidth"
      >
        <el-input v-model="dialogForm.address" autocomplete="off" />
      </el-form-item>
    </el-form>
    <!-- 新增 -->
    <div class="exam_from_btn_box">
      <el-button type="danger" size="mini" @click="backBtn()">返回</el-button>
      <el-button type="primary" @click="confirmBtn()"> 确认 </el-button>
    </div>
  </div>
</template>

<script>
import Api from "../../api/index";
export default {
  name: "",
  data() {
    return {
      dialogForm: {
        id: null,
        title: "",
        image_path: "",
        address: "",
        start_date: "",
      },

      formLabelWidth: "100px",
      baseAPIUrl: this.$store.state.baseAPIUrl, //默认路径
    };
  },
  mounted() {
    console.log(this.$route.query);
    this.dialogForm.id = this.$route.query.id;
    this.dialogForm.title = this.$route.query.title;
    this.dialogForm.image_path = this.$route.query.image_path;
    this.dialogForm.start_date = this.$route.query.start_date;
    this.dialogForm.address = this.$route.query.address;
  },
  methods: {
    // 自定义上传图片
    async uploadFile(e) {
      //   console.log(e);
      let formData = new FormData();
      formData.append("source", 5);
      formData.append("file", e.file);
      let res = await Api.uploadFile(formData);
      if (res.code == 0) {
        console.log(this.baseAPIUrl + res.file_path);
        this.dialogForm.image_path = this.baseAPIUrl + res.file_path;
        console.log(this.dialogForm.image_path, "sjdsjdjsdj");
      } else {
        this.$message.error(res.code.msg);
      }
    },
    // 上传成功，获取返回的图片地址
    handleUpImage(res) {
      console.log(res);
    },
    // 上传前，限制的上传图片大小
    beforeImageUpload(file) {
      console.log(file);
      if (file.size / 1024 / 1024 > 1) {
        console.log("000");
        this.$message.error("上传图片大小不能超过 10MB!");
        return false;
      }
      return true;
    },
    //新增
    confirmBtn() {
      if (this.dialogForm.title == "") {
        this.$message.error("请输入新增标题");
        return false;
      }
      if (this.dialogForm.image_path == "") {
        this.$message.error("请新增图片");
        return false;
      }
      if (this.dialogForm.start_date == "") {
        this.$message.error("请输入新增比赛时间");
        return false;
      }
      if (this.dialogForm.address == "") {
        this.$message.error("请输入新增比赛");
        return false;
      }
      if (this.dialogForm.id == null) {
        let url = "/admin/signin/store";
        let data = {
          title: this.dialogForm.title,
          image_path: this.dialogForm.image_path,
          start_date: this.dialogForm.start_date,
          address: this.dialogForm.address,
        };
        this.axios
          .post(url, data)
          .then((res) => {
            if (res.code == 0) {
              this.$router.replace({
                name: "signlist",
              });
            } else {
              this.$message.error(res.msg);
            }
          })
          .catch(() => {
            this.$message({
              message: "新增失败",
            });
          });
      }
    },
    // 返回上一页
    backBtn() {
      this.$router.replace({
        name: "signlist",
      });
    },
  },
};
</script>

<style scoped>
.exam_from_btn_box {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  bottom: 0;
  left: 0;
  padding: 10px 0;
  border-top: 1px solid #dddddd;
  background-color: #fff;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>